﻿using Microsoft.Extensions.Logging;
using PHMEE.Entities.DaqInfo.TdDevices;
using PHMEE.IRepositories.DaqInfo.TdDevices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Guids;
using Volo.Abp.Uow;

namespace PHMEE
{
    /// <summary>
    /// 生成能效TD种子数据
    /// </summary>
    public class EeDataSeedManager : ITdDataSeedManager
    {
        public DbPrefixEnum DatabaseType => DbPrefixEnum.Ee;

        private readonly ILogger<EeDataSeedManager> _logger;
        private readonly IGuidGenerator _guidGenerator;
        private readonly ITdDataBaseRepository _tdDatabaseRepository;
        private readonly ITdDeviceTypeRepository _tdDeviceTypeRepository;
        private readonly ITdDeviceAttributeRepository _tdDeviceAttributeRepository;
        private readonly ITdDeviceInstanceRepository _tdDeviceInstanceRepository;

        public EeDataSeedManager(
            ILogger<EeDataSeedManager> logger,
            IGuidGenerator guidGenerator,
            ITdDataBaseRepository tdDatabaseRepository,
            ITdDeviceTypeRepository tdDeviceTypeRepository,
            ITdDeviceAttributeRepository tdDeviceAttributeRepository,
            ITdDeviceInstanceRepository tdDeviceInstanceRepository)
        {
            _logger = logger;
            _guidGenerator = guidGenerator;
            _tdDatabaseRepository = tdDatabaseRepository;
            _tdDeviceTypeRepository = tdDeviceTypeRepository;
            _tdDeviceAttributeRepository = tdDeviceAttributeRepository;
            _tdDeviceInstanceRepository = tdDeviceInstanceRepository;
        }

        [UnitOfWork]
        public async Task<bool> SeedDataAsync(Guid vesselId, string dbDescription)
        {

            try
            {
                //数据库
                string dbName = PhmeeConst.GetDbName(DatabaseType, vesselId);

                var databaseId = _guidGenerator.Create();
                var database = new TdDatabase(databaseId)
                {
                    Name = dbName,
                    Description = dbDescription
                };
                await _tdDatabaseRepository.InsertAsync(database, autoSave: true);

                // 创建超级表、设备属性、子表数据
                var superDeviceTypes = CreateSuperDeviceTypes(databaseId);
                await _tdDeviceTypeRepository.InsertManyAsync(superDeviceTypes);

                var attributes = CreateDeviceAttributes(superDeviceTypes);
                await _tdDeviceAttributeRepository.InsertManyAsync(attributes);

                var subDeviceInstances = CreateDeviceInstances(superDeviceTypes);
                await _tdDeviceInstanceRepository.InsertManyAsync(subDeviceInstances);

                return true;
            }
            catch (Exception ex)
            {
                _logger.LogError($"生成种子数据错误: {ex.Message}");
                return false;
            }
        }
        // 创建超级表
        private List<TdDeviceType> CreateSuperDeviceTypes(Guid databaseId)
        {
            return new List<TdDeviceType>
            {
                new TdDeviceType(_guidGenerator.Create())
                {
                    Name = "shaft_power_meter",
                    Description = "轴功率计设备",
                    DatabaseId = databaseId
                },
                new TdDeviceType(_guidGenerator.Create())
                {
                    Name = "gps",
                    Description = "GPS设备",
                    DatabaseId = databaseId
                },
                new TdDeviceType(_guidGenerator.Create())
                {
                    Name = "flowmeter",
                    Description = "流量计设备",
                    DatabaseId = databaseId
                }
            };
        }

        // 创建设备属性
        private List<TdDeviceAttribute> CreateDeviceAttributes(List<TdDeviceType> superDeviceTypes)
        {
            var superSpmTableId = superDeviceTypes.First(d => d.Name == "shaft_power_meter").Id;
            var superGpsTableId = superDeviceTypes.First(d => d.Name == "gps").Id;
            var superFmTableId = superDeviceTypes.First(d => d.Name == "flowmeter").Id;

            return new List<TdDeviceAttribute>
            {
                // 轴功率计
                new TdDeviceAttribute(_guidGenerator.Create()) { Name = "ts", Description = "时间戳", ColumnType = "TIMESTAMP", TdDeviceTypeId = superSpmTableId, SortOrder = 0 },
                new TdDeviceAttribute(_guidGenerator.Create()) { Name = "speed", Description = "轴转速", ColumnType = "FLOAT", TdDeviceTypeId = superSpmTableId, SortOrder = 1 },
                new TdDeviceAttribute(_guidGenerator.Create()) { Name = "power", Description = "轴功率", ColumnType = "FLOAT", TdDeviceTypeId = superSpmTableId, SortOrder = 2 },
                new TdDeviceAttribute(_guidGenerator.Create()) { Name = "torque", Description = "轴扭矩", ColumnType = "FLOAT", TdDeviceTypeId = superSpmTableId, SortOrder = 3 },
                new TdDeviceAttribute(_guidGenerator.Create()) { Name = "total_revs", Description = "总转数", ColumnType = "BIGINT UNSIGNED", TdDeviceTypeId = superSpmTableId, SortOrder = 4 },
                new TdDeviceAttribute(_guidGenerator.Create()) { Name = "total_eng", Description = "总能量", ColumnType = "BIGINT UNSIGNED", TdDeviceTypeId = superSpmTableId, SortOrder = 5 },

                // GPS
                new TdDeviceAttribute(_guidGenerator.Create()) { Name = "ts", Description = "时间戳", ColumnType = "TIMESTAMP", TdDeviceTypeId = superGpsTableId, SortOrder = 0 },
                new TdDeviceAttribute(_guidGenerator.Create()) { Name = "latitude", Description = "纬度", ColumnType = "FLOAT", TdDeviceTypeId = superGpsTableId, SortOrder = 1 },
                new TdDeviceAttribute(_guidGenerator.Create()) { Name = "longitude", Description = "经度", ColumnType = "FLOAT", TdDeviceTypeId = superGpsTableId, SortOrder = 2 },
                new TdDeviceAttribute(_guidGenerator.Create()) { Name = "ground_speed", Description = "对地速度", ColumnType = "FLOAT", TdDeviceTypeId = superGpsTableId, SortOrder = 3 },
                new TdDeviceAttribute(_guidGenerator.Create()) { Name = "distance", Description = "航程", ColumnType = "FLOAT", TdDeviceTypeId = superGpsTableId, SortOrder = 4 },

                // 流量计
                new TdDeviceAttribute(_guidGenerator.Create()) { Name = "ts", Description = "时间戳", ColumnType = "TIMESTAMP", TdDeviceTypeId = superFmTableId, SortOrder = 0 },
                new TdDeviceAttribute(_guidGenerator.Create()) { Name = "total_value", Description = "总流量", ColumnType = "FLOAT", TdDeviceTypeId = superFmTableId, SortOrder = 1 },
                new TdDeviceAttribute(_guidGenerator.Create()) { Name = "oil_value", Description = "分钟油耗", ColumnType = "FLOAT", TdDeviceTypeId = superFmTableId, SortOrder = 2 },
                new TdDeviceAttribute(_guidGenerator.Create()) { Name = "instant", Description = "即时油耗", ColumnType = "FLOAT", TdDeviceTypeId = superFmTableId, SortOrder = 3 },
                new TdDeviceAttribute(_guidGenerator.Create()) { Name = "density", Description = "密度", ColumnType = "FLOAT", TdDeviceTypeId = superFmTableId, SortOrder = 4 },
                new TdDeviceAttribute(_guidGenerator.Create()) { Name = "temperature", Description = "温度", ColumnType = "FLOAT", TdDeviceTypeId = superFmTableId, SortOrder = 5 },
                new TdDeviceAttribute(_guidGenerator.Create()) { Name = "fuel_type", Description = "油品", ColumnType = "INT", TdDeviceTypeId = superFmTableId, SortOrder = 6 }
            };
        }

        // 创建设备实例
        private List<TdDeviceInstance> CreateDeviceInstances(List<TdDeviceType> superDeviceTypes)
        {
            var superSpmTableId = superDeviceTypes.First(d => d.Name == "shaft_power_meter").Id;
            var superGpsTableId = superDeviceTypes.First(d => d.Name == "gps").Id;
            var superFmTableId = superDeviceTypes.First(d => d.Name == "flowmeter").Id;

            return new List<TdDeviceInstance>
            {
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "spm_001", Description = "1#轴功率计", TdDeviceTypeId = superSpmTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "gps_001", Description = "1#gps", TdDeviceTypeId = superGpsTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "fm_001", Description = "主副机进油", TdDeviceTypeId = superFmTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "fm_002", Description = "主副机回油", TdDeviceTypeId = superFmTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "fm_003", Description = "副机进油", TdDeviceTypeId = superFmTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "fm_004", Description = "副机回油", TdDeviceTypeId = superFmTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "fm_005", Description = "锅炉进油", TdDeviceTypeId = superFmTableId },
                new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "fm_006", Description = "锅炉回油", TdDeviceTypeId = superFmTableId }
            };
        }

    }
}
